package com.zsk.copy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static java.lang.System.out;
import static java.lang.System.err;

/**
 * 复制文件夹并加后缀_
 * 
 * @author zincredible
 * @date 2019/04/02 14:17:06
 */
public class MyDeconde {

	
	public static void main(String[] args) throws IOException {
		args=new String[]{"",""};
		if (args == null || args.length != 2) {
			showErrorMsg("输入的参数有误");
			return;
		}
		String sourceDir = args[0];
		String targetDir = args[1];
		sourceDir="F:\\vuePrj\\my-blog\\";
		targetDir="F:\\vuePrj\\my-blog_\\";
		decodeFile(sourceDir, targetDir);
		
//		String sourceDir = "E:\\AMSphereRepository\\code\\";
//		String targetDir = "E:\\AMSphereRepositoryWorkspace\\";
//		File tagFile = new File(targetDir);
//		tagFile.delete();
//		tagFile.mkdirs();
//		List<String> fileList = new ArrayList<String>();
//		listFiles(sourceDir, fileList);
//		for (String filePath : fileList) {
//			if (filePath.contains("\\src") || filePath.endsWith("pom.xml")) {
//
//				File tmpFile = new File(filePath.replace(sourceDir, targetDir) + "_");
//				if (!tmpFile.getParentFile().isDirectory()) {
//					tmpFile.getParentFile().mkdirs();
//				}
//				copyFile(new File(filePath), tmpFile);
//			}
//		}
//		out.println("finish");
	}
	
	public static void decodeFile(String sourceDir, String targetDir) {
		
		File sourceFile = new File(sourceDir);
		if (!sourceFile.isDirectory()) {
			showErrorMsg(sourceDir + "不是一个文件夹");
			return;
		}
		File tagFile = new File(targetDir);
		tagFile.delete();
		tagFile.mkdirs();
		List<String> fileList = new ArrayList<String>();
		listFiles(sourceDir, fileList);
		for (String filePath : fileList) {
			//if (filePath.contains("\\src") || filePath.endsWith("pom.xml")) {
			if (!filePath.contains("node_modules")) {
			
				File tmpFile = new File(filePath.replace(sourceDir, targetDir) + "_");
				if (!tmpFile.getParentFile().isDirectory()) {
					tmpFile.getParentFile().mkdirs();
				}
				copyFile(new File(filePath), tmpFile);
			}
		}
		showInfo("======success========");
	}

	public static List<String> getFileList(String dir) {
		List<String> fileList = new ArrayList<String>();
		listFiles(dir, fileList);
		return fileList;
	}

	/**
	 * 获取文件列表
	 * 
	 * @param dir
	 * @param fileList
	 * @author: zhangsike
	 * @date: 2018/11/28 10:51:56
	 */
	public static void listFiles(String dir, List<String> fileList) {
		File file = new File(dir);
		File[] array = file.listFiles();

		for (int i = 0; i < array.length; i++) {
			if (array[i].isFile()) {
				try {
					fileList.add(array[i].getCanonicalPath());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else if (array[i].isDirectory() && !array[i].getPath().contains("node_modules")) {
				listFiles(array[i].getPath(), fileList);
			}
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param fromFile
	 * @param toFile
	 */
	public static void copyFile(File fromFile, File toFile) {

		FileInputStream ins = null;
		FileOutputStream out = null;
		try {
			showInfo(fromFile.getName() + "==>" + toFile.getName());
			ins = new FileInputStream(fromFile);
			out = new FileOutputStream(toFile);
			byte[] b = new byte[1024];
			int n = 0;
			while ((n = ins.read(b)) != -1) {
				out.write(b, 0, n);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ins != null) {
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	private static void showErrorMsg(String msg) {
		err.println(msg);
		err.println("=========failure============");
	}

	private static void showInfo(Object o) {
		out.println(o);
	}


}
